package language.basic.oopBase;

import java.io.*;

public class cloneTest {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        B b = new B();
        Object clone = b.clone();
        System.out.println(b);
        System.out.println(clone);

        B b1 = b.deepClone();
        System.out.println(b1);
    }
}
class B implements Cloneable, Serializable{
    public String a="sad";

    public B deepClone() throws IOException, ClassNotFoundException {
        // 将对象写到流里
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bo);
        oo.writeObject(this);

        // 从流里读出来
        InputStream bi = new ByteArrayInputStream(bo.toByteArray());
        ObjectInputStream oi = new ObjectInputStream(bi);
        return (B)(oi.readObject());
    }

    @Override
    public B clone(){
        try {
            return (B) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }
}

class C extends A {}